package com.company.base.web.filter;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.company.core.utils.IpUtil;


public class LogFilter implements Filter{
	
	private Logger sysLogger = LoggerFactory.getLogger("system");
	
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void doFilter(ServletRequest arg0, ServletResponse arg1,
			FilterChain chain) throws IOException, ServletException {
		HttpServletRequest request = (HttpServletRequest)arg0;
		HttpServletResponse response = (HttpServletResponse)arg1;
	
		// 
		String ip = IpUtil.getIp(request);
		String userAgent = request.getHeader("User-Agent");
		String referer = request.getHeader("Referer");
		
		HttpSession session = request.getSession();

		// 
		String uId = null;
//		if( comUser != null ) {
//			uId = String.valueOf(comUser.getId());
//		} else if (TerminalDetector.isSpider(userAgent)) {
//			uId = "-"+String.valueOf(TerminalDetector.getSpiderType(userAgent)) ;
//		}

		//
		int comUserType = 0;
//		if ( comUser!=null && comUser.getComUserType() != null ) {
//			comUserType = comUser.getComUserType() ;
//		}

		//
		sysLogger.info("sid={}\tuid={}\turi={}\tip={}\tua={}\tref={}\tut={}", 
				new String[]{session.getId(), uId, request.getRequestURI(), ip, 
				userAgent, referer, String.valueOf(comUserType)});
		
		chain.doFilter(request, response);
		
	}

	@Override
	public void init(FilterConfig arg0) throws ServletException {
		// TODO Auto-generated method stub
		
	}

}
